import {getUserInfo} from "@/api/login.js";
import {load_user_tow} from "@/api/useryk.js";
import {
	LOGIN_STATUS,
	UID,
	PLATFORM,
	USER_INFO,
	GLOBAL_DATA,
	LOGIN_STATUS_USER,
	UID_USER,
	USER_INFOU,
	VID
} from '@/config/store';
import Cache from '@/utils/cache';

const state = {
	token: Cache.get(LOGIN_STATUS) || '',//群主token
	user_token: Cache.get(LOGIN_STATUS_USER) || '',//会员token
	// user_token: '199811ab_user',//会员token
	// token: '199811ab',// 群主token
	backgroundColor: "#fff",
	userInfo: Cache.get(USER_INFO)?JSON.parse(Cache.get(USER_INFO)):null,//群主信息
	userInfou: Cache.get(USER_INFOU)?JSON.parse(Cache.get(USER_INFOU)):null,//会员信息
	uid: Cache.get(UID) || null,//群主id
	user_uid: Cache.get(UID_USER) || null,//会员id
	homeActive: false,
	chatUrl: Cache.get('chatUrl') || '',
	systemPlatform: Cache.get(PLATFORM)?Cache.get(PLATFORM):'',//设备
	productType: Cache.get('productType') || '',
	vipRow: Cache.get('vipRow') || '',  
	kefuTell: Cache.get('kefuTell') || '',
	sharePic: Cache.get('sharePic') || '',
	weixinLogin: Cache.get('weixinLogin') || '',
	globalData:Cache.get(GLOBAL_DATA) || {},//公共信息
	vid:Cache.get(VID) || null,//视频id
    shopData: Cache.get('shopData') || {},
};

const mutations = {
	//群主token
	LOGIN(state, opt) {
		state.token = opt.token;
		Cache.set(LOGIN_STATUS, opt.token);
	},
	// 会员token
	LOGINUSER(state, opt) {
		state.user_token = opt.token;
		Cache.set(LOGIN_STATUS_USER, opt.token);
	},
	// 群主id
	SETUID(state,val){                                                
		state.uid = val;
		Cache.set(UID, val);
	},
	//会员id
	SETUIDUSER(state,val){                                                
		state.user_uid = val;
		Cache.set(UID_USER, val);
	},
	//群主token
	UPDATE_LOGIN(state, token) {
		state.token = token;
		Cache.set(LOGIN_STATUS, token);
	},
	// 会员token
	UPDATE_LOGIN_USER(state, token) {
		state.user_token = token;
		Cache.set(LOGIN_STATUS_USER, token);
	},
	// 群主登录
	LOGOUT(state) {
		state.token = undefined;
		state.uid = undefined
		Cache.clear(LOGIN_STATUS);
		Cache.clear(UID);
		Cache.clear(USER_INFO);
	},
	// 会员登录
	LOGOUT_USER(state) {
		state.user_token = undefined;
		state.user_uid = undefined
		Cache.clear(LOGIN_STATUS_USER);
		Cache.clear(UID_USER);
		Cache.clear(USER_INFOU);
	},
	// 群主信息
	UPDATE_USERINFO(state, userInfo) {
		state.userInfo = userInfo;
		Cache.set(USER_INFO, userInfo);
	},
	// 会员信息
	UPDATE_USERINFO_USER(state, userInfo) {
		state.userInfou = userInfo;
		Cache.set(USER_INFOU, userInfo);
	},
	// AuthorizeType(state, authorizeType){
	// 	state.authorizeType = authorizeType;
	// },
	//更新群主useInfo数据
	changInfo(state, payload) {
		state.userInfo[payload.amount1] = payload.amount2;
		Cache.set(USER_INFO, state.userInfo);
	},
	//更新会员useInfo数据
	changUserInfo(state, payload) {
		state.userInfou[payload.amount1] = payload.amount2;
		Cache.set(USER_INFOU, state.userInfou);
	},
	// 获取视频id
	SETVID(state,val){
		state.vid = val;
		Cache.set(VID, val);
	},
	
	
	
	//商品类型，用于区分视频号商品与一般商品
	PRODUCT_TYPE(state, productType) {
		state.productType = productType;
		Cache.set('productType', productType);
	},
	// 购买会员信息
	VIP_ROW(state, row) {
		state.vipRow = row;
		Cache.set('vipRow', row);
	},
	// 公共信息
	GET_GLOBAL(state, globalData) {
		state.globalData = globalData;
		Cache.set('GLOBAL_DATA', globalData);
	},
	// 设备类型
	SYSTEM_PLATFORM(state, systemPlatform){
		state.systemPlatform = systemPlatform;
		Cache.set(PLATFORM, systemPlatform);
	},
	SET_CHATURL(state, chatUrl){
		state.chatUrl = chatUrl;
	},
	SET_KFTELL(state, kefuTell){
		state.kefuTell = kefuTell;
	},
	SET_SHAREPIC(state, sharePic){
		state.sharePic = sharePic;
	},
	SET_WXLOGIN(state, weixinLogin){
		state.weixinLogin = weixinLogin;
	},
	// OPEN_HOME(state) {
	// 	state.homeActive = true;
	// },
	// CLOSE_HOME(state) {
	// 	state.homeActive = false;
	// },
	BACKGROUND_COLOR(state, color) {
		state.color = color;
		document.body.style.backgroundColor = color;
	},
    setShopData(state, data){
        state.shopData = data
        Cache.set('shopData', data);
    }
};

const actions = {
	USERINFO({
		state,
		commit
	}, force) {
		return new Promise(reslove => {
			load_user_tow().then(res => {
				commit("UPDATE_USERINFO_USER", res.data);
				reslove(res.data);
			});
		}).catch(() => {
		
		});
	}
};

export default {
	state,
	mutations,
	actions
};
